const basiCode = require('../util/baseCode.js');



// 添加版本， 
exports.addVersion = async (ctx,resultz) => {
	 const { data: { icon,name,description,template_id,template_version,mainUserOpenId }, cloud } = ctx
	 const {appkey,app_id,app_name,app_description,app_icon,pre_view_url,app_version} = resultz
	try {
		//先判断是否存在当前主用户id
		let select_mainUserOpenId_sql =	await cloud.dataspace.executeSql(`SELECT * from converse_versions WHERE mainUserOpenId = "${mainUserOpenId}";`);

		if(!select_mainUserOpenId_sql.length){
				await cloud.dataspace.executeSql(`INSERT INTO converse_versions (gmt_create,gmt_modified,appkey,app_id,app_name,app_description,app_icon,pre_view_url,app_version,mainUserOpenId) VALUES('${curentTime()}','${curentTime()}','${appkey}','${app_id}','${app_name}','${app_description}','${app_icon}','${pre_view_url}','${app_version}','${mainUserOpenId}');`);
				return { isOk: 0, msg:"实例化成功已经插入"};
	}else{
			return { isOk: 1, msg:{sub_msg:"插入失败"}};
		}


	} catch (err) {
		return err;
	}
}

//查询
exports.getVersion = async (ctx,mainUserOpenId) => {
	  const { data ,cloud } = ctx
	try {
		let find_sql	= await cloud.dataspace.executeSql(`SELECT * from converse_versions WHERE mainUserOpenId = "${mainUserOpenId}";`);

	  
		return await basiCode(0, { isOk: 1, msg: "查询成功",data:find_sql});


	} catch (err) {
		return err;
	}
}



//获取线上版本号
exports.getonline_Version = async (ctx,mainUserOpenId) => {
	  const { data ,cloud } = ctx
	try {
		let find_sql	= await cloud.dataspace.executeSql(`select template_version,template_id from converse_versions_get`);

	  
		return await basiCode(0, { isOk: 1, msg: "查询成功",data:find_sql[0]});


	} catch (err) {
		return err;
	}
}




//更新
exports.update_Version = async (ctx) => {
	  const { data ,cloud,mainUserOpenId } = ctx
	try {
		let find_sql	= await cloud.dataspace.executeSql(`select template_version from converse_versions_get`);
		//find_sql[0].version	 
	  


		let update_sql	= await cloud.dataspace.executeSql(`update converse_versions set app_version = '${find_sql[0].template_version}' where mainUserOpenId = '${mainUserOpenId}';`);
		return  { isOk: 1, msg:"更新成功",data:update_sql};


	} catch (err) {
		return err;
	}
}










// 埋点
exports.addUserStat = async (params, time, cloud) => {
	try {
		console.log("==============try==================")
		let query = `insert into tracking_message_record (data, gmt_create, gmt_modified) VALUES ('${params}', '${time}', now())`;
		let data = await cloudfn(cloud, query);
		return data;
	} catch (err) {
		console.log("==============err==================", err)
		return err;
	}
},

cloudfn = async (cloud, sql)=> {
	console.log("==============sql==================", sql)
	let cbData = await cloud.dataspace.executeSql(sql, [])
	console.log("==============cbData==================", cbData)
	return cbData
}

//获取东八区时间
var curentTime = function (time) {
    var d = time ? new Date(time) : new Date();
    var localTime = d.getTime();
    var localOffset = d.getTimezoneOffset() * 60000; //获得当地时间偏移的毫秒数
    var utc = localTime + localOffset + 3600000 * 8;
    var now = new Date(utc);

    var year = now.getFullYear(); //年
    var month = now.getMonth() + 1; //月
    var day = now.getDate(); //日
    var hh = now.getHours(); //时
    var mm = now.getMinutes(); //分
    var ss = now.getSeconds(); //秒

    var clock = year + "-";

    if (month < 10)
        clock += "0";

    clock += month + "-";

    if (day < 10)
        clock += "0";

    clock += day + " ";

    if (hh < 10)
        clock += "0";

    clock += hh + ":";

    if (mm < 10)
        clock += "0";

    clock += mm + ":";

    if (ss < 10)
        clock += "0";

    clock += ss;

    return (clock);
}



